/**************************************************************************************
*  Rtc.java
*  Program demonstrating the use real time clock with LPC21xx.
**************************************************************************************
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Created by:
*      RTJ Computing
**************************************************************************************/
import java.util.*;
import lpc2xxx.hw.*;


class Rtc
{
    static void main(String[] args) 
    {
        Console.println("");
        Console.println("LPC21xx Real Time Clock");

        showMenu();
        RTC.init(false);
        while (true)
        {
            char key = Console.getKey();
            if (key == Console.ESC)
                break;

            switch (key) 
            {
                case Console.CR:
                    showMenu();
                    break;
                case '1':
                    showDateTime();
                    break;
                case '2':
                    setDate();
                    break;
                case '3':
                    setTime();
                    break;
            }
        }
    }

    static void showMenu()
    {
        Console.println("");
        Console.println("  1 - show current date/time");
        Console.println("  2 - change date");
        Console.println("  3 - change time");
        Console.println("ESC - exit");
    }

    static void showDateTime()
    {
        Console.println("");
        Console.println(RTC.getFullDate());
        int lastSec = -1;
        while (true)
        {
            char key = Console.getKey();
            if (key == Console.ESC || key == Console.CR)
                break;

            int s = RTC.getSec();
            if (lastSec != s)
            {
                lastSec = s;
                Console.print("\r" + RTC.getFullTime());
            }
            try 
            {
                Thread.sleep(100);
            }
            catch (Exception e) {}
        }
        Console.println("");
        showMenu();
    }

    static void setDate()
    {
        String invalDate = "\nInvalid date value or format";
        
        Console.println("");
        Console.println("Current date: " + RTC.getFullDate());
        
        while (true)
        {
            Console.print("Enter new date as W-DD-MM-YYYY (W=week day, 0=Monday): ");
            String input = Console.getKeys();
            if (input == null)
                break;
            StringTokenizer tok = new StringTokenizer(input, "-");
            if (tok.countTokens() < 4)
                Console.println(invalDate);
            else
            {
                int w, d, m, y;
                try 
                {
                    w = Integer.parseInt(tok.nextToken());
                    d = Integer.parseInt(tok.nextToken());
                    m = Integer.parseInt(tok.nextToken());
                    y = Integer.parseInt(tok.nextToken());
                    RTC.setDoW(w);
                    RTC.setDay(d);
                    RTC.setMon(m);
                    RTC.setYear(y);
                    Console.println("\nNew date set");
                    break;
                }
                catch (NumberFormatException e)
                {
                    Console.println(invalDate);
                }
            }
        }
        Console.println("");
        showMenu();
    }

    static void setTime()
    {
        String invalTime = "\nInvalid time value or format";
        
        Console.println("");
        while (true)
        {
            Console.print("Enter new time as HH:MM:SS (24 hour format): ");
            String input = Console.getKeys();
            if (input == null)
                break;
            StringTokenizer tok = new StringTokenizer(input, ":");
            if (tok.countTokens() < 3)
                Console.println(invalTime);
            else
            {
                int h, m, s;
                try 
                {
                    h = Integer.parseInt(tok.nextToken());
                    m = Integer.parseInt(tok.nextToken());
                    s = Integer.parseInt(tok.nextToken());
                    RTC.setHour(h);
                    RTC.setMin(m);
                    RTC.setSec(s);
                    Console.println("\nNew time set");
                    break;
                }
                catch (NumberFormatException e)
                {
                    Console.println(invalTime);
                }
            }
        }
        Console.println("");
        showMenu();
    }

}

